/**
 * @ author: 汪孟苹
 */
define(["jquery","bbcare"],function ($,$bb) {
    var servers = {};
    var URL = $bb.baseUrl("followup");

    /**
	 * 获取上一个页面传递过来的参数（workOrderId）
	 */
    servers.getPageParams = function(){
    	return $bb.getPageParams();
    }

    /**
     * 请求月度评估记录基本详情
     */
    servers.ajaxAssessDetailInfo = function(params,callback){
    	$bb.ajax({
			url: URL + "followup/service/patient/evaluation/workorder/detail",
			data:JSON.stringify({
				data : {
					"workOrderId":params.workOrderId
				}
			}),
			success: function(data){
				if(data.success){ 
					callback(data.data,params);
				}else{
					$bb.toastShow(data.msg ? data.msg : "请求月度评估记录基本详情出错！");
				}
			}
		});
    }

    /**
     * 处理宝宝基本信息数据
     */
    servers.handleInfo = function(data){
    	data.gender = data.gender == 'F' ? "女" : "男";
    	var weeks = Math.floor(data.pregnancyDay / 7);
    	var days = data.pregnancyDay % 7;
    	data.pregnancyDay = days == 0 ? weeks + "周" : weeks + "周" + days + "天";
    	data.createTime = data.createTime.substr(0,10);
    	return data;
    }

    /**
     * 请求评估记录结果信息(包含指导意见）
     */
    servers.ajaxWorkorderResult = function(params,callback){
    	$bb.ajax({
			url: URL + "followup/service/patient/evaluation/workorder/result",
			data:JSON.stringify({
				data : params
			}),
			success: function(data){
				if(data.success){ 
					callback(data.data,params);
				}else{
					$bb.toastShow(data.msg ? data.msg : "请求评估记录结果信息出错！");
				}
			}
		});
    }

    /**
     * 处理获得的评估记录结果信息(包含指导意见）
     */
    servers.handleFillAssessData = function(data){
        if( data.type == "01" ){
            if (data.guide) {
                data.result.heightMap.analysis = data.guide.height;
                data.result.weightMap.analysis = data.guide.weight;
                data.result.headtMap.analysis = data.guide.head;
            }
        }else if( data.type == "02" ){
            if( data.result ){ // data.result存在，即该宝宝做了智力发育评估
                data.result.arr = [];
                data.result.arr.push(data.result.Communicate);
                data.result.arr.push(data.result.BigAction);
                data.result.arr.push(data.result.FineAction);
                data.result.arr.push(data.result.SolveProblem);
                data.result.arr.push(data.result.PersonSocial);
                data.result.flag = true;
                if(data.guide) data.result.suggest = data.guide.suggest;
                data.result.arrAotuGuide = [];
                var obj1 = {};
                obj1.text = data.aotuGuide.Communicate;
                obj1.key = data.result.Communicate.desc;
                data.result.arrAotuGuide.push(obj1);
                var obj2 = {};
                obj2.text = data.aotuGuide.BigAction;
                obj2.key = data.result.BigAction.desc;
                data.result.arrAotuGuide.push(obj2);
                var obj3 = {};
                obj3.text = data.aotuGuide.FineAction;
                obj3.key = data.result.FineAction.desc;
                data.result.arrAotuGuide.push(obj3);
                var obj4 = {};
                obj4.text = data.aotuGuide.SolveProblem;
                obj4.key = data.result.SolveProblem.desc;
                data.result.arrAotuGuide.push(obj4);
                var obj5 = {};
                obj5.text = data.aotuGuide.PersonSocial;
                obj5.key = data.result.PersonSocial.desc;
                data.result.arrAotuGuide.push(obj5);
            }else{
                data.result = {};
                data.result.flag = false;
            }
        }else if( data.type == "03" ){
            if( data.result ){ // data.result存在，即该宝宝做了养育环境评估
                data.result.arr = [];
                data.result.arr.push(data.result.ActivitySpace);
                data.result.arr.push(data.result.ActivityDiversity);
                data.result.arr.push(data.result.HeavySportsToys);
                data.result.arr.push(data.result.FineSportsToys);
                data.result.flag = true;
                if(data.guide) data.result.suggest = data.guide.suggest;
                data.result.arrAotuGuide = [];
                var obj1 = {};
                obj1.text = data.aotuGuide.ActivitySpace;
                obj1.key = data.result.ActivitySpace.desc;
                data.result.arrAotuGuide.push(obj1);
                var obj2 = {};
                obj2.text = data.aotuGuide.ActivityDiversity;
                obj2.key = data.result.ActivityDiversity.desc;
                data.result.arrAotuGuide.push(obj2);
                var obj3 = {};
                obj3.text = data.aotuGuide.HeavySportsToys;
                obj3.key = data.result.HeavySportsToys.desc;
                data.result.arrAotuGuide.push(obj3);
                var obj4 = {};
                obj4.text = data.aotuGuide.FineSportsToys;
                obj4.key = data.result.FineSportsToys.desc;
                data.result.arrAotuGuide.push(obj4);
            }else{
                data.result = {};
                data.result.flag = false;
            }
        }else if( data.type == "04" ){
            // 不需要处理数据
        }else if( data.type == "05" ){
            var arr = ["优秀","良好","一般","较差"];
            if( !data.guide ) data.guide = {};
            data.guide.list = arr;
            for (var i = 0; i < data.guide.list.length; i++) {
                var str = data.guide.list[i];
                data.guide.list[i] = {};
                data.guide.list[i].val = str;
                data.guide.list[i].flag = str == data.guide.evaluate ? true : false;
            }
        }else if( data.type == "06" ){
            for (var i = 0; i < data.data.doctorList.length; i++) {
                data.data.doctorList[i].flag = data.data.doctorList[i].doctorId == data.doctorId ? true : false;
            }
        }else if( data.type == "07" ){
            data.data = {};
            data.data.workOrderId = data.workOrderId;
            var monthlyAssessReport = $bb.link("monthlyAssessReport");
            data.data.reportLink = monthlyAssessReport;
        }
    	return data;
    }

    /**
     * 点击上一步、下一步时，根据原有的type，找到需要的type
     */
    servers.getType = function(type,operation){
        var arr = ["01","02","03","04","05","06","07"]; // 评估类型
        for (var i = 0; i < arr.length; i++) {
            if( arr[i] == type){
                if(operation == "pre"){
                    return arr[i-1];
                }else if(operation == "next"){
                    return arr[i+1];
                }
            } 
        }
    }

    /**
     * 提交或修改评估指导意见
     */
    servers.ajaxManualguideModify = function(params,callback){
        $bb.ajax({
            url: URL + "followup/service/patient/evaluation/manualguide/modify",
            data:JSON.stringify({
                data : params
            }),
            success: function(data){
                if(data.success){ 
                    callback(data.data,params);
                }else{
                    $bb.toastShow(data.msg ? data.msg : "请提交评估指导意见出错！");
                }
            }
        });
    }

    /**
     * 请求医生列表
     */
    servers.ajaxDoctorList = function(params,callback){
        $bb.ajax({
            url: URL + "followup/service/patient/evaluation/doctor/list",
            success: function(data){
                if(data.success){ 
                    callback(data,params);
                }else{
                    $bb.toastShow(data.msg ? data.msg : "请求医生列表出错！");
                }
            }
        });
    }

    /**
     * 选择医生
     */
    servers.ajaxDoctorModify = function(params,callback,detailData){
        $bb.ajax({
            url: URL + "followup/service/patient/evaluation/workorder/doctormodify",
            data:JSON.stringify({
                data : {
                    "doctorId":params.doctorId,
                    "workOrderId":params.workOrderId
                }
            }),
            success: function(data){
                if(data.success){ 
                    callback(data,params,detailData);
                }else{
                    $bb.toastShow(data.msg ? data.msg : "选择医生出错！");
                }
            }
        });
    }

    return servers;
});